JavaScript

A5.u.objectget Method

Syntax

A5.u.object.get(object,path[,type[,format]])

Arguments

objectobjectarray

The object or array to get the value in.

pathstringarray

The path to the value to get. If a string is passed in, the string should be formatted as it would be is accessed through javascript (for example "customer[1].name"). If an array, each subsequent entry in the array is the next leaf to access (such as ['customer',1,'name'']).

typestring

Enforced type of return value. Values can be "date", "number", "boolean", "string", "string-date" or "string-number".

formatstring

The format for a "date", "number", "string-date" or "string-number" type.

Returns

valueany

The raw, or enforced typed value.

Description

Get a value in an object or array from the passed in path.

Discussion

The A5.u.object.get method allows for values to be retrieved from an object or array based on a passed in path. If the path is not valid, then a null value will be returned. The type of the returned value can be enforced by setting the "type" argument.

Valid type arguments are "date", "number", "boolean", "string", "string-date" and "string-number".

A type of "date" will try to parse the value using the passed in date format (see [Date.fromFormat]), unless it is already a date. If the value is not a valid date then a new date object will be returned.

A type of "number" will return the value as a number. If the value cannot be turned into a number a value of zero will be returned.

A type of "boolean" will return the value as a boolean (see A5.d.bool for truthy and falsey values).

A type of "string" will return the value as a string. If a "-date" is added and the original value is a date, then the passed in date format will be used to convert the date into a string. If a "-number" is added and the original value is a number, then the passed in number format will be used to convert the number into a string.

Example

var data = {customer: [{name: 'Bob', dob: '1976.3.2'},{name: 'Fred', dob: '1981.4.15'}]};
var name = A5.u.object.get(data,'customer[1].name');
// name = 'Fred';
var dob = A5.u.object.get(data,'customer[1].dob','date','yyyy.MM.dd');
// dob = Date(1981,3,15);